package com.miaoxie.forum.module.designPatterns.simpleFactory;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: miaoxie
 *
 * 简单的创建水果工厂实例
 *
 *          1.工厂(Creator) 角色
 *          简单工厂模式的核心，它负责实现创建所有实例的内部逻辑。工
 *          厂类可以被外界直接调用，创建所需的产品对象。
 *
 * Date: 2018-09-07
 * Time: 上午8:50
 */
public  class FruitFactory {

    public static Fruit getByClass(Class c) throws InstantiationException {
        if (Fruit.class.isAssignableFrom(c)){
            try {
                return (Fruit) c.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }else {
            throw new InstantiationException("Can't find "+c.getClass().getName()+"");
        }
        return null;
    }



}
